function [ avgTimes, avgConfusionMatrix  ] = RunAllClassifications(functionNames, iterations, trainingLabels, trainingSize, data, test_data, kernel_handle, plotFlag )

if (iterations > 1)
    plotFlag = 0;
end

numFunctions = size(functionNames, 1);
numTrainLabels = size(trainingLabels,1);
allLabels = unique(data(:,5));
numAllLabels = length(allLabels);


sumTimes = zeros(numFunctions,numTrainLabels);
sumConfusionMatrix = cell(numFunctions, numTrainLabels);
for i = 1:numFunctions
    for j = 1:numTrainLabels
        sumConfusionMatrix{i,j} = zeros(numAllLabels+1, 2+1);
    end
end

orig_data = data;

for i = 1:iterations
    
    p = randperm(size(orig_data,1));
    data = orig_data(p,:);
    
    for j = 1:numTrainLabels
        
        numFromEachClass = round(trainingSize/(2*(numAllLabels-1)))*ones(numAllLabels,1);
        numFromEachClass(trainingLabels(j)==allLabels) = round(trainingSize/2);
        
        [ TrainingData, ~ ] = SelectData( data, numFromEachClass, trainingSize); 
        
        TrainingData = TrainingData(randperm(size(TrainingData,1)), :);
%         TestData = TestData(randperm(size(TestData,1)), :);

        for k = 1:numFunctions
            
            [ predictions, confidence, time, confusionMatrix, onlineError ] = TestClassificationFunction(functionNames{k}, kernel_handle, TrainingData, test_data, trainingLabels(j));
            sumConfusionMatrix{k,j} = sumConfusionMatrix{k,j} + [NaN, trainingLabels(j), -trainingLabels(j); confusionMatrix];
            sumTimes(k,j) = sumTimes(k,j) + time;
            
            if (plotFlag)
                filename = ['../results/' functionNames{k} '_' num2str(trainingLabels(j)) '.wrl'];
                PrintVRMLColorful( filename, test_data, trainingLabels(j), predictions );
            end
        end
    end
end

avgConfusionMatrix = cell(numFunctions, numTrainLabels);
for k = 1:numFunctions
    for j = 1:numTrainLabels
    avgConfusionMatrix{k,j} = sumConfusionMatrix{k,j}/iterations;
    end
end
avgTimes = sumTimes/iterations;

end

